﻿﻿
using System;
using System.Data;
using System.IO;
using Microsoft.VisualBasic.FileIO;
using System.Text;
using OfficeOpenXml;


namespace DataTableUtilities
{
    public class MyClient
    {
        public static void run()
        {
            // string f_path = @"/Users/mac/Desktop/操作记录表.csv";
            // string f_path = @"data/图书表.csv";
            // string f_path = @"data/上传路径选项.csv";

            // Example usage of the function
            DataTable dt = new DataTable();
            dt.Columns.Add("Column1", typeof(string));
            dt.Columns.Add("Column2", typeof(string));
            dt.Rows.Add("Row1 Column1", "Row1 Column2222");
            dt.Rows.Add("Row2 Column1", "Row2 Column2222333");

            string f_path_0 = @"data/test_save_to_excel.xlsx";
            DataTableSaver.to_excel(dt, f_path_0);

            // string f_path = @"data/save/test_save_to_csv.csv";
            string f_path = @"data/save/test_save_to_csv.csv";
            // string dir_path = Path.GetDirectoryName(f_path);
            // if (!Directory.Exists(dir_path))
            //     Directory.CreateDirectory(dir_path);
            DataTableSaver.to_csv(dt, f_path);
        }
    }
    public static class DataTableSaver
    {
        public static void to_excel(DataTable dt, string file_path)
        {
            using (ExcelPackage excel = new ExcelPackage())
            {
                ExcelWorksheet worksheet = excel.Workbook.Worksheets.Add("Sheet1");
                worksheet.Cells["A1"].LoadFromDataTable(dt, true);
                excel.SaveAs(new FileInfo(file_path));
            }
        }
        public static void to_csv(DataTable dt, string file_path)
        {
            if (!File.Exists(file_path))
                File.Create(file_path).Close();

            using (TextFieldParser parser = new TextFieldParser(file_path, Encoding.UTF8))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                using (StreamWriter sw = new StreamWriter(file_path))
                {
                    int columnCount = dt.Columns.Count;
                    for (int i = 0; i < columnCount; i++)
                    {
                        sw.Write(dt.Columns[i]);
                        if (i < columnCount - 1)
                        {
                            sw.Write(",");
                        }
                    }
                    sw.Write(sw.NewLine);
                    foreach (DataRow dr in dt.Rows)
                    {
                        for (int i = 0; i < columnCount; i++)
                        {
                            if (!Convert.IsDBNull(dr[i]))
                            {
                                sw.Write(dr[i].ToString());
                            }
                            if (i < columnCount - 1)
                            {
                                sw.Write(",");
                            }
                        }
                        sw.Write(sw.NewLine);
                    }
                }
            }
        }
    }
}
